#include <basic_types.h>
#include <mediatypes.h>

#include <api/libc/printf.h>
#include <hld/osd/osddrv.h>
#include <api/libsubt/subt_osd.h>
#include <api/libsubt/lib_subt.h>
//#include "osd_lib.h"
#include <hld/hld_dev.h>
#include <hld/sdec/sdec.h>
#include <hld/dis/vpo.h>

extern struct subt_config_par g_subt_config_par;//
extern struct osd_device *osd_dev;

extern void tve_cc_onoff(BOOL on);

#define CC_PRINTF	soc_printf
#define CC_CHAR_W		14
#define CC_CHAR_H		20

#define OSD_CC_WIDTH   	  480//(CC_CHAR_W*40+16)//560//704(16bytes aligned)
#define OSD_CC_HEIGHT  	  300//(CC_CHAR_H*15)//500//570

#define CC_COLOR_NUMBER 256
#define CC_OSD_MODE 	OSD_256_COLOR
#define CC_BITNUM		8
#define CC_FACTOR		0

#define CC_TRANSPARENT	0x08
#define CC_BLACK	0x07

static  BOOL cc_show_on;
static UINT8 m_bCurRegion;
static VSCR CC_Vscr ;
static UINT8 CC_VscrBuf[OSD_CC_WIDTH *OSD_CC_HEIGHT];
static const UINT8 cc_pallette[4*256] = {
				/*front*/
				0xea, 0x7f, 0x7f, 0x0f,	//white
				0x8f, 0x37, 0x24, 0x0f,	//green
				0x27, 0xec, 0x6e, 0x0f,	//blue
				0xa7, 0xa3, 0x12, 0x0f,	//cyan
				0x50, 0x5b, 0xec, 0x0f,	//red
				0xd0, 0x12, 0x90, 0x0f,	//yellow
				0x68, 0xc7, 0xda, 0x0f,	//magenta
				0x10, 0x80, 0x80, 0x0f,	//black 
				/*back*/
				0, 0, 0, 0,	//transparent
};
static const UINT32 CC_Font[96][9] = 
{
	//Latin G0 set, 0x20~0x7f maps to 0x20~0x7f
 	{ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000},    //  032  0x20
       { 0x00000000, 0x0001c007, 0x001c0070, 0x01c00700, 0x1c007001, 0xc0070000, 0x007001c0, 0x07000000, 0x00000000},    //  033  0x21
       { 0x00000000, 0x001f7c7d, 0xf1f7c3cf, 0x0f3c79c1, 0xe7000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000},    //  034  0x22
       { 0x00000000, 0x000f3c3c, 0xf0f3c7ff, 0x9ffe7ff8, 0xf3c3cf0f, 0x3c7ff9ff, 0xe3cf0f3c, 0x3cf00000, 0x00000000},    //  035  0x23
	{0x00000000, 0x3000c01f, 0xc0ffc3cf, 0x0f001f00, 0x1f001f00, 0x7c00f0f3, 0xc1fc01e0, 0x03000c00, 0x00000000},
       { 0x00000000, 0x00000038, 0x19f0e6c7, 0x9b1c7ce0, 0xe7003803, 0xc01ee0f7, 0xc71b187c, 0x40e00000, 0x00000000},    //  037  0x25
       { 0x00000000, 0x0000003f, 0x01fe0618, 0x18606181, 0x8603f00f, 0xc0739b87, 0xec0e3ffe, 0x7f980000, 0x00000000},    //  038  0x26
       { 0x00000000, 0x0003e00f, 0x803e0078, 0x01e00f00, 0x3c000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000},    //  039  0x27
       { 0x00000000, 0x00000001, 0xc01f00f0, 0x03c01f00, 0x7801e007, 0x801e007c, 0x00f001f0, 0x01c00000, 0x00000000},    //  040  0x28
       { 0x00000000, 0x0000001c, 0x007c0078, 0x01e007c0, 0x0f003c00, 0xf003c01f, 0x007807c0, 0x1c000000, 0x00000000},    //  041  0x29
	{0x01e00e00, 0x7001c000, 0x00000000, 0x03de3ff9, 0xf3e7879e, 0x1e7879f3, 0xe7cf8ffe, 0x0f780000, 0x00000000},
       { 0x00000000, 0x00000000, 0x001e0078, 0x01e00781, 0xffc7ff1f, 0xfc07801e, 0x00780000, 0x00000000, 0x00000000},    //  043  0x2b
       { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x000000f0, 0x03c00f00, 0x1c00e003, 0x800e0000},    //  044  0x2c
       { 0x00000000, 0x00000000, 0x00000000, 0x00000001, 0xffc7ff1f, 0xfc000000, 0x00000000, 0x00000000, 0x00000000},    //  045  0x2d
       { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x000000f0, 0x03c00f00, 0x3c000000, 0x00000000},    //  046  0x2e
       { 0x00000000, 0x00000000, 0x3001c00f, 0x007803c0, 0x1e00f007, 0x803c01e0, 0x0f003800, 0x00000000, 0x00000000},    //  047  0x2f
       { 0x00000000, 0x00000007, 0x807f83cf, 0x0f3c3cf0, 0xf3c3cf0f, 0x3c3cf0f3, 0xc3cf07f8, 0x07800000, 0x00000000},    //  048  0x30
       { 0x00000000, 0x00000003, 0xc01f00fc, 0x07f01fc0, 0x0f003c00, 0xf003c00f, 0x003c00f0, 0x03c00000, 0x00000000},    //  049  0x31
       { 0x00000000, 0x0000000f, 0xc07f83ff, 0x0e3c38f0, 0x07803c01, 0xe00f0078, 0x03ff0ffc, 0x3ff00000, 0x00000000},    //  050  0x32
       { 0x00000000, 0x00000007, 0x807f03ce, 0x0e1c3870, 0x03c03c00, 0xf000f0e1, 0xc3ce07f0, 0x07800000, 0x00000000},    //  051  0x33
       { 0x00000000, 0x00000001, 0xc00f007c, 0x03f01fc0, 0xf7071c1c, 0x707ff1ff, 0xc01c0070, 0x01c00000, 0x00000000},    //  052  0x34
       { 0x00000000, 0x0000007f, 0xe1ff8700, 0x1c007001, 0xff07fe00, 0x7800e1c3, 0x87de0ff0, 0x1f800000, 0x00000000},    //  053  0x35
       { 0x00000000, 0x0000000f, 0xc07f8186, 0x0e003800, 0xfe03fc0e, 0x3838e0e3, 0x818e07f0, 0x0f800000, 0x00000000},    //  054  0x36
       { 0x00000000, 0x0000003f, 0xe0ff8006, 0x003800e0, 0x07001800, 0xe0070018, 0x00e00300, 0x0c000000, 0x00000000},    //  055  0x37
       { 0x00000000, 0x0000000f, 0x807f038e, 0x0e3838e0, 0xe381fc07, 0xf038e0e3, 0x838e07f0, 0x0f800000, 0x00000000},    //  056  0x38
       { 0x00000000, 0x0000000f, 0x807f038e, 0x0e3838e0, 0xe3838e07, 0xf80fe003, 0x830e0ff0, 0x1f800000, 0x00000000},    //  057  0x39
       { 0x00000000, 0x00000000, 0x003c00f0, 0x03c00f00, 0x3c000000, 0x000f003c, 0x00f003c0, 0x00000000, 0x00000000},    //  058  0x3a
       { 0x00000000, 0x00000000, 0x003c00f0, 0x03c00f00, 0x3c000000, 0x000f003c, 0x00f003c0, 0x1f007800, 0x00000000},    //  059  0x3b
       { 0x00000000, 0x00001c01, 0xf01fc0fc, 0x07e01f83, 0xf80f803f, 0x801f803f, 0x007f007c, 0x00700000, 0x00000000},    //  060  0x3c
       { 0x00000000, 0x00000000, 0x01ffc7ff, 0x1ffc7ff0, 0x0007ff1f, 0xfc7ff000, 0x00000000, 0x00000000, 0x00000000},    //  061  0x3d
       { 0x00000000, 0x003c00f8, 0x03f801f8, 0x03f00fc0, 0x1fc01f01, 0xfc0fc07e, 0x0fe03e00, 0xf0000000, 0x00000000},    //  062  0x3e
       { 0x00000000, 0x000ff83f, 0xf1ffc78f, 0x003c00f0, 0x03c07f01, 0xf8070000, 0x007001c0, 0x07000000, 0x00000000},    //  063  0x3f
       { 0x00000000, 0x0003fc3f, 0xf1e1efbf, 0xbcfef3fb, 0xddef77bd, 0xdef3fbef, 0xe7801ffc, 0x3ff00000, 0x00000000},    //  064  0x40
       { 0x00000000, 0x00000007, 0x001c01f8, 0x07e01f80, 0xf783de1e, 0x3c78f3ff, 0xefffb81e, 0xc0180000, 0x00000000},    //  065  0x41
       { 0x00000000, 0x0000003f, 0x80ff03ce, 0x0f383ce0, 0xf383fc0f, 0xf83c70f1, 0xc3c70ff8, 0x3fc00000, 0x00000000},    //  066  0x42
       { 0x00000000, 0x0000001f, 0x80ff03ce, 0x1e1c7871, 0xe007801e, 0x007801e1, 0xc3ce0ff0, 0x1f800000, 0x00000000},    //  067  0x43
       { 0x00000000, 0x0000007f, 0x81ff071c, 0x1c3c70f1, 0xc3c70f1c, 0x3c70f1c3, 0xc71c1ff0, 0x7f800000, 0x00000000},    //  068  0x44
       { 0x00000000, 0x0000007f, 0xe1ff8700, 0x1c007001, 0xc007fc1f, 0xf07001c0, 0x07001ff8, 0x7fe00000, 0x00000000},    //  069  0x45
       { 0x00000000, 0x0000003f, 0xe0ff83c0, 0x0f003c00, 0xf003fc0f, 0xf03c00f0, 0x03c00f00, 0x3c000000, 0x00000000},    //  070  0x46
       { 0x00000000, 0x0000000f, 0xc0ff838f, 0x9c1e7079, 0xc007001c, 0x7e71f9c1, 0xe38f8ffe, 0x0fc00000, 0x00000000},    //  071  0x47
       { 0x00000000, 0x00000070, 0x71c1c707, 0x1c1c7071, 0xc1c7ff1f, 0xfc7071c1, 0xc7071c1c, 0x70700000, 0x00000000},    //  072  0x48
       { 0x00000000, 0x0000000f, 0x003c00f0, 0x03c00f00, 0x3c00f003, 0xc00f003c, 0x00f003c0, 0x0f000000, 0x00000000},    //  073  0x49
       { 0x00000000, 0x00000000, 0xe003800e, 0x003800e0, 0x03800e00, 0x3870e1c3, 0x878e0ff0, 0x1f800000, 0x00000000},    //  074  0x4a
       { 0x00000000, 0x00000078, 0x39e1c78e, 0x1e707b81, 0xfc07e01f, 0xc07f81e7, 0x078e1e1c, 0x78380000, 0x00000000},    //  075  0x4b
       { 0x00000000, 0x0000001c, 0x007001c0, 0x07001c00, 0x7001c007, 0x001c0070, 0x01c007fc, 0x1ff00000, 0x00000000},    //  076  0x4c
       { 0x00000000, 0x0000007c, 0x7df1f7ef, 0xdfbf7efd, 0xfbf7ffde, 0xe77b9dee, 0x7791de47, 0x791c0000, 0x00000000},    //  077  0x4d
       { 0x00000000, 0x00000070, 0x39e0e7c3, 0x9f8e7e39, 0xfce7ff9e, 0xfe79f9e3, 0xe7839e0e, 0x78380000, 0x00000000},    //  078  0x4e
       { 0x00000000, 0x00000007, 0x80ffc3cf, 0x1e1e7879, 0xe1e7879e, 0x1e7879e1, 0xe3cf0ffc, 0x07800000, 0x00000000},    //  079  0x4f
       { 0x00000000, 0x0000007f, 0xe1ffc78f, 0x1e3c78f1, 0xe3c7ff1f, 0xf87801e0, 0x07801e00, 0x78000000, 0x00000000},    //  080  0x50
       { 0x00000000, 0x00000007, 0x80ffc3cf, 0x1e1e7879, 0xe1e7879e, 0x1e7b79ed, 0xe3df0ffc, 0x07f80060, 0x00000000},    //  081  0x51
       { 0x00000000, 0x0000007f, 0xc1ffc78f, 0x1e3c78f1, 0xe3c7fc1f, 0xe07bc1e7, 0xc78f9e1e, 0x78380000, 0x00000000},    //  082  0x52
       { 0x00000000, 0x0000001f, 0x80ff078f, 0x1e1c7801, 0xfe03fe07, 0xfc00f1c3, 0xc79f0ff8, 0x1fc00000, 0x00000000},    //  083  0x53
       { 0x00000000, 0x000000ff, 0xfbffe0f0, 0x03c00f00, 0x3c00f003, 0xc00f003c, 0x00f003c0, 0x0f000000, 0x00000000},    //  084  0x54
       { 0x00000000, 0x00000070, 0x79c1e707, 0x9c1e7079, 0xc1e7079c, 0x1e7079c1, 0xe7cf8ff8, 0x1fc00000, 0x00000000},    //  085  0x55
       { 0x00000000, 0x000000e0, 0x7b81e70e, 0x1c383dc0, 0xf703dc07, 0xe01f803c, 0x00f00080, 0x02000000, 0x00000000},    //  086  0x56
       { 0x00000000, 0x000000e7, 0x3b9cee73, 0xb9cee73b, 0x9cee73bb, 0xeefdfbe3, 0xef0fbc1e, 0x60300000, 0x00000000},    //  087  0x57
       { 0x00000000, 0x000000e0, 0x7bc3e79e, 0x0ff01f80, 0x7e00f003, 0xc01f80ff, 0x079e3c3c, 0xe0700000, 0x00000000},    //  088  0x58
       { 0x00000000, 0x000000e0, 0x73c3c79e, 0x0ff01f80, 0x7e00f003, 0xc00f003c, 0x00f003c0, 0x0f000000, 0x00000000},    //  089  0x59
       { 0x00000000, 0x0000007f, 0xe1ff800e, 0x007803c0, 0x1e00f007, 0x803c01e0, 0x07001ff8, 0x7fe00000, 0x00000000},    //  090  0x5a
       { 0x00000000, 0x7e01f807, 0x801e0078, 0x01e00780, 0x1e007801, 0xe007801e, 0x007801f8, 0x07e00000, 0x00000000},    //  091  0x5b
	{0x03c01e00, 0xf003c000, 0x00000000, 0x07e03fc3, 0xef8f0e3f, 0xf8ffe3c0, 0x0f000ff0, 0x1f800000, 0x00000000},
       { 0x00000001, 0xf007c007, 0x001c0070, 0x01c00700, 0x1c007001, 0xc007001c, 0x007007c0, 0x1f000000, 0x00000000},    //  093  0x5d
	{0x01c00e00, 0x70000000, 0x00000000, 0x00000700, 0x1c007001, 0xc007001c, 0x007001c0, 0x07000000, 0x00000000},
	{0x03c01e00, 0xf0000000, 0x00000000, 0x07e03fc1, 0xef870e1c, 0x3870e1ef, 0x87be0ff0, 0x1f800000, 0x00000000},
	{0x01c01e00, 0xf0000000, 0x00000000, 0x1c3870e1, 0xc3870e1c, 0x3870e1c3, 0x87fe1ff8, 0x3fc00000, 0x00000000},
       { 0x00000000, 0x00000000, 0x00000000, 0x07de3ff9, 0xf3e7cf9e, 0x1e7879e1, 0xe7cf8ffe, 0x1f780000, 0x00000000},    //  097  0x61
       { 0x00000000, 0x00000078, 0x01e00780, 0x1fe07fc1, 0xf3c7cf1e, 0x3c78f1e3, 0xc7cf1ff0, 0x7f800000, 0x00000000},    //  098  0x62
       { 0x00000000, 0x00000000, 0x00000000, 0x01f01ff0, 0xffc3c70e, 0x003800f1, 0xc3ff07fc, 0x07c00000, 0x00000000},    //  099  0x63
       { 0x00000000, 0x00000000, 0x7001c007, 0x03dc1ff0, 0xffc3870e, 0x1c3870e1, 0xc3ff07fc, 0x0f700000, 0x00000000},    //  100  0x64
       { 0x00000000, 0x00000000, 0x00000000, 0x03f01fe0, 0xf3c3870f, 0xfc3ff0e0, 0x03c707fc, 0x0fe00000, 0x00000000},    //  101  0x65
       { 0x00000000, 0x00000003, 0xc03f00f0, 0x03c01fc0, 0x7f01fc03, 0xc00f003c, 0x00f003c0, 0x0f000000, 0x00000000},    //  102  0x66
       { 0x00000000, 0x00000000, 0x00000000, 0x03ee3ff8, 0xe3e38f9c, 0x1e7078e3, 0xe3ff87fe, 0x0078e1e3, 0xff07f800},    //  103  0x67
       { 0x00000000, 0x00000070, 0x01c00700, 0x1c007fc1, 0xff87fe1c, 0x3870e1c3, 0x870e1c38, 0x70e00000, 0x00000000},    //  104  0x68
       { 0x00000000, 0x0000000f, 0x003c0000, 0x00000f00, 0x3c00f003, 0xc00f003c, 0x00f003c0, 0x0f000000, 0x00000000},    //  105  0x69
       { 0x00000000, 0x0000000f, 0x003c0000, 0x00000f00, 0x3c00f003, 0xc00f003c, 0x00f003c0, 0x0f003c03, 0xf00f8000},    //  106  0x6a
       { 0x00000000, 0x000000f0, 0x03c00f00, 0x3cf0f783, 0xfc0fe03f, 0x00fc03fc, 0x0f783cf0, 0xf0e00000, 0x00000000},    //  107  0x6b
       { 0x00000000, 0x0000000e, 0x003800e0, 0x03800e00, 0x3800e003, 0x800e0038, 0x00e00380, 0x0e000000, 0x00000000},    //  108  0x6c
       { 0x00000000, 0x00000000, 0x00000000, 0x3ff8fff3, 0xccef33bc, 0xcef33bcc, 0xef33bcce, 0xf3380000, 0x00000000},    //  109  0x6d
       { 0x00000000, 0x00000000, 0x00000000, 0x1ff07ff1, 0xe3c78f1e, 0x3c78f1e3, 0xc78f1e3c, 0x78f00000, 0x00000000},    //  110  0x6e
       { 0x00000000, 0x00000000, 0x00000000, 0x03e03fe1, 0xf7c78f1e, 0x3c78f1e3, 0xc7df0ff8, 0x0f800000, 0x00000000},    //  111  0x6f
       { 0x00000000, 0x00000000, 0x00000000, 0x1de07fe1, 0xf3c7cf1e, 0x3c78f1e3, 0xc7cf1ff8, 0x7f81e007, 0x801e0000},    //  112  0x70
       { 0x00000000, 0x00000000, 0x00000000, 0x07dc3ff1, 0xe7c79f1e, 0x3c78f1e3, 0xc79f0ffc, 0x1ff003c0, 0x0f003c00},    //  113  0x71
       { 0x00000000, 0x00000000, 0x00000000, 0x07b81fe0, 0x7f01f007, 0x801e0078, 0x01e00780, 0x1e000000, 0x00000000},    //  114  0x72
       { 0x00000000, 0x00000000, 0x00000000, 0x07f83ff0, 0xe1c3800f, 0xf81ff001, 0xc3870ffc, 0x1fe00000, 0x00000000},    //  115  0x73
       { 0x00000000, 0x00000003, 0x003c00f0, 0x07f01fc0, 0x3c00f003, 0xc00f003c, 0x00f003f0, 0x03c00000, 0x00000000},    //  116  0x74
       { 0x00000000, 0x00000000, 0x00000000, 0x1c3870e1, 0xc3870e1c, 0x3870e1c3, 0x870e1ff8, 0x3fc00000, 0x00000000},    //  117  0x75
       { 0x00000000, 0x00000000, 0x00000000, 0x1e3c78f0, 0xf783de07, 0x701fc03e, 0x00f801c0, 0x02000000, 0x00000000},    //  118  0x76
       { 0x00000000, 0x00000000, 0x00000000, 0x3bdeef7b, 0xbdeef7bb, 0xdeef7bbd, 0xeef7bffe, 0x79e00000, 0x00000000},    //  119  0x77
       { 0x00000000, 0x00000000, 0x00000000, 0x1c1c78f0, 0xff83fe07, 0xf00f807f, 0x03fe1e3c, 0x70700000, 0x00000000},    //  120  0x78
       { 0x00000000, 0x00000000, 0x00000000, 0x1c3870e0, 0xef03bc0e, 0xf01fc07e, 0x00f803e0, 0x07003c03, 0xc00e0000},    //  121  0x79
       { 0x00000000, 0x00000000, 0x00000000, 0x0ff03fc0, 0x0f003c01, 0xe00f0070, 0x03800ff0, 0x3fc00000, 0x00000000},    //  122  0x7a
	{0x00000000, 0x0000001f, 0x81ff079e, 0x3c18f003, 0xc00f003c, 0x00f063c1, 0x879e1ff0, 0x1f80380f, 0xe03f8000},
	{0x00000000, 0x0003c00f, 0x003c0000, 0x00007fe1, 0xff87fe1f, 0xf8000000, 0x00f003c0, 0x0f000000, 0x00000000},
{ 0x0000f003, 0xe003800e, 0x003800e0, 0x03e007c0, 0x1f007c03, 0xe00e0038, 0x00e00380, 0x3e00f000, 0x00000000},    //(not requested)  125  0x7d
{ 0x1c31f9cf, 0x7e38f000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000},    //(not requested)  126  0x7e
       { 0x0001ffe7, 0xff9ffe7f, 0xf9ffe7ff, 0x9ffe7ff9, 0xffe7ff9f, 0xfe7ff9ff, 0xe7ff8000, 0x00000000, 0x00000000}    //  127  0x7f
};

void cc_osd_enter()
{
    	struct OSDPara	tOpenPara;
	struct OSDRect	tOpenRect;
	struct OSDRect  r;
	enum OSDSys  eOSDSys;
	enum TVSystem tvsys;


	vpo_ioctl((struct vpo_device *)dev_get_by_type(NULL, HLD_DEV_TYPE_DIS), VPO_IO_GET_OUT_MODE, (UINT32)(&tvsys));
    
	
	tOpenPara.eMode= OSD_256_COLOR;
	tOpenPara.uGAlphaEnable= FALSE;	
	tOpenPara.uGAlpha= 0x0f;
	tOpenPara.uPalletteSel = 0;

	osd_dev = (struct osd_device *)dev_get_by_id(HLD_DEV_TYPE_OSD, 1);

	OSDDrv_Close((HANDLE)osd_dev);
	OSDDrv_Open((HANDLE)osd_dev, &tOpenPara);
	osal_task_sleep(20);
	OSDDrv_SetPallette((HANDLE)osd_dev,(UINT8*)cc_pallette, CC_COLOR_NUMBER, OSDDRV_YCBCR);

	tOpenRect.uLeft = (720 - OSD_CC_WIDTH)>>1;
	tOpenRect.uWidth = OSD_CC_WIDTH;
	
	if(tvsys == NTSC||tvsys == PAL_M||tvsys == PAL_60||tvsys == NTSC_443)	
		tOpenRect.uTop = (480 - OSD_CC_HEIGHT)>>1;
	else
		tOpenRect.uTop = (570 - OSD_CC_HEIGHT)>>1;

	tOpenRect.uHeight = OSD_CC_HEIGHT;

	OSDDrv_CreateRegion((HANDLE)osd_dev, m_bCurRegion, &(tOpenRect), NULL);


	CC_Vscr.vR.uLeft = 0;
	CC_Vscr.vR.uTop = 0;
	CC_Vscr.vR.uWidth = tOpenRect.uWidth;
	CC_Vscr.vR.uHeight = tOpenRect.uHeight;
	
	CC_Vscr.lpbScr = CC_VscrBuf;
	MEMSET(CC_VscrBuf,CC_TRANSPARENT,(CC_Vscr.vR.uWidth)*(CC_Vscr.vR.uHeight));
	
	r.uLeft = 0;
	r.uTop = 0;
	r.uWidth =tOpenRect.uWidth;
	r.uHeight =tOpenRect.uHeight;
	
	OSDDrv_RegionFill((HANDLE)osd_dev, m_bCurRegion, &r, CC_TRANSPARENT);


    if(tvsys == NTSC||tvsys == PAL_M||tvsys == PAL_60||tvsys == NTSC_443)
		eOSDSys = OSD_NTSC;
	else
		eOSDSys = OSD_PAL;

	//OSDDrv_Scale((HANDLE)osd_dev,OSD_VSCALE_CC_SUBT,(UINT32)(&eOSDSys));
	OSDDrv_ShowOnOff((HANDLE)osd_dev, OSDDRV_ON);

	cc_show_on = TRUE;	   
}

void cc_osd_leave()
{
	enum OSDSys  eOSDSys;
	enum TVSystem tvsys;
	cc_show_on = FALSE;
	vpo_ioctl((struct vpo_device *)dev_get_by_type(NULL, HLD_DEV_TYPE_DIS), VPO_IO_GET_OUT_MODE, (UINT32)(&tvsys));

	osd_dev = (struct osd_device *)dev_get_by_id(HLD_DEV_TYPE_OSD, 1);
    	OSDDrv_DeleteRegion((HANDLE)osd_dev, m_bCurRegion);


    if(tvsys == NTSC||tvsys == PAL_M||tvsys == PAL_60||tvsys == NTSC_443)
		eOSDSys = OSD_NTSC;
	else
		eOSDSys = OSD_PAL;
	//OSDDrv_Scale((HANDLE)osd_dev,OSD_VSCALE_OFF,(UINT32)(&eOSDSys));
	OSDDrv_ShowOnOff((HANDLE)osd_dev, OSDDRV_OFF);
		   
}

void CC_DrawChar(UINT16 x, UINT16 y, UINT8 fg_color, UINT8 bg_color,UINT8 double_width,UINT8 double_height, UINT8 character)
{
	UINT8* pData; 
	struct OSDRect r;
      INT16 dw,dh;
	UINT8 charset_idx, char_idx;
	UINT32 charmask;
	UINT16 bit_count;
	UINT8 i, j, k;
	UINT8 data1, data2;
	UINT8 zoomkind;
	UINT8 temp;
	UINT32*  char_addr;


	if(cc_show_on!=TRUE)
		return;

	x= x*CC_CHAR_W;
	y= y*CC_CHAR_H;
	bg_color = CC_BLACK;
	if(character < 0x20 ||character > 0x7f)
		return;
	if((x%CC_CHAR_W) != 0 || (y%CC_CHAR_H) != 0 || x+CC_CHAR_W > OSD_CC_WIDTH  || y+CC_CHAR_H > OSD_CC_HEIGHT)
	{
		CC_PRINTF("-----------------CC_DrawChar(), parameter illegal,x=%d,y=%d\n",x,y);
		return ;
	}
		
	//set CC_Vscr size
	
	r.uLeft = x;
	r.uTop = y;
	r.uWidth = CC_CHAR_W;
	r.uHeight = CC_CHAR_H;
		

	pData = CC_Vscr.lpbScr+CC_Vscr.vR.uWidth*y+x;
			

	bit_count = 0;
	//char_addr = find_char( character);
	char_addr = (UINT32*)(CC_Font[character - 0x20]);
	
		//vertical normal , horizine normal
     	for(i = 0 ;i < 9;i++)
     	{
     		charmask = 0x80000000;	
		for(j = 0 ;j < 32 ;j++)
		{
			if(bit_count >= CC_CHAR_W * CC_CHAR_H)	//char draw finished
				break;
		
			if(bit_count!=0 && bit_count %14 ==0)	//one row finished, change to next
				pData += (CC_Vscr.vR.uWidth-14);
			//16 color, 2 pixels per byte 

			if(*char_addr & charmask)
				data1 = fg_color;
			else
				data1 = bg_color;
	
			*pData = data1;

			pData++;
			bit_count ++;
			charmask = charmask>>1;
											
		}
		char_addr += 1;

     	}

	// write Vscr data to osd frame buffer
	OSDDrv_RegionWrite((HANDLE)osd_dev, m_bCurRegion, &CC_Vscr, &r);
}

void CC_FillChar(UINT16 x, UINT16 y,UINT16 w,UINT16 h,UINT8 bg_color)
{
	INT32 i;
	struct OSDRect r;	
	UINT8* pData; 
	if(cc_show_on!=TRUE)
		return;

	bg_color = CC_TRANSPARENT;

	r.uLeft = x*CC_CHAR_W;
	r.uTop = y*CC_CHAR_H;
	r.uWidth = w*CC_CHAR_W;
	r.uHeight = h*CC_CHAR_H;		

	pData = CC_Vscr.lpbScr+CC_Vscr.vR.uWidth*y*CC_CHAR_H+x*CC_CHAR_W;
	for(i=0;i<h*CC_CHAR_H;i++)
	{
		MEMSET(pData+CC_Vscr.vR.uWidth*i,bg_color,w*CC_CHAR_W);
	}
	OSDDrv_RegionFill((HANDLE)osd_dev, m_bCurRegion, &r,bg_color); 
	
}

void CC_CarriageReturn(UINT8 start_row,UINT8 end_row)
{	
	if(cc_show_on!=TRUE)
		return;	
	UINT8 *from,*to;
	from = CC_Vscr.lpbScr+CC_Vscr.vR.uWidth*(start_row+1)*CC_CHAR_H;
	to = CC_Vscr.lpbScr+CC_Vscr.vR.uWidth*start_row*CC_CHAR_H;
	MEMCPY(to,from,CC_Vscr.vR.uWidth*(end_row-start_row)*CC_CHAR_H);
	MEMSET(CC_Vscr.lpbScr+CC_Vscr.vR.uWidth*end_row*CC_CHAR_H,CC_TRANSPARENT,CC_Vscr.vR.uWidth*CC_CHAR_H);
	struct OSDRect r;
	r.uLeft = 0;
	r.uTop = start_row*CC_CHAR_H;
	r.uWidth = CC_Vscr.vR.uWidth;
	r.uHeight = (end_row-start_row+1)*CC_CHAR_H;
	OSDDrv_RegionWrite((HANDLE)osd_dev, m_bCurRegion, &CC_Vscr, &r);
}

INT32 cc_show_onoff(BOOL bOn)
{
	//LIBSUBT_PRINTF("cc= %x\n",bOn);
    if(g_subt_config_par.cc_by_vbi == TRUE)
	    tve_cc_onoff(bOn);

    if(g_subt_config_par.cc_by_osd== TRUE)
    {
	    if(bOn)
		    cc_osd_enter();
	    else
		    cc_osd_leave();
    }
}
